//
//  APMGapBorderView.m
//  FXManager
//
//  Created by lcj on 2020/12/2.
//  Copyright © 2020 华旅电子. All rights reserved.
//

#import "APMGapBorderView.h"

@implementation APMGapBorderView

- (void)setStrokeColorAndWidth:(CGContextRef)context {
    CGFloat lineWidth = self.gapBorderWidth > 0 ? self.gapBorderWidth : 1.0;
    UIColor * lineColor = self.gapBorderColor ? self.gapBorderColor : [UIColor lightGrayColor];
    CGContextSetLineWidth(context, lineWidth);
    CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
}

- (void)drawTopRect:(CGRect)rect{
    CGFloat contentW = CGRectGetWidth(rect);
    CGFloat contentH = CGRectGetHeight(rect);
    if (self.startGap.x > 0) {
        if (self.startGap.y == 0) {
            if (self.startGap.x < self.endGap.x) {
                /*
                 ┌────    ─┐
                 │         │
                 └─────────┘
                 或者
                 ┌────
                 │         │
                 └─────────┘
                 */
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                if (self.endGap.x < contentW) {
                    CGContextMoveToPoint(context, self.endGap.x, 0);
                    CGContextAddLineToPoint(context, contentW, 0);
                } else {
                    CGContextMoveToPoint(context, contentW, 0);
                }
                CGContextAddLineToPoint(context, contentW, contentH);
                CGContextAddLineToPoint(context, 0, contentH);
                CGContextAddLineToPoint(context, 0, 0);
                CGContextAddLineToPoint(context, self.startGap.x, 0);
                CGContextStrokePath(context);
            } else if (self.endGap.x == 0){
                /*
                 ─────
                 */
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                CGContextMoveToPoint(context, 0, 0);
                CGContextAddLineToPoint(context, self.startGap.x, 0);
                CGContextStrokePath(context);
            }
        } else if (self.startGap.y > 0){
            if (self.startGap.y == contentH) {
                /*
                 ─────────┐
                          │
                         ─┘
                或者
                 ─────────┐
                          │
                */
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                if (self.endGap.x < contentW) {
                    CGContextMoveToPoint(context, self.endGap.x, 0);
                    CGContextAddLineToPoint(context, contentW, 0);
                } else {
                    CGContextMoveToPoint(context, contentW, 0);
                }
                CGContextAddLineToPoint(context, contentW, contentH);
                if (self.startGap.x < contentW) {
                    CGContextAddLineToPoint(context, self.startGap.x, contentH);
                }
                CGContextStrokePath(context);
            } else {
                if (self.startGap.x == contentW) {
                    /*
                    ──
                    或者
                    ─┐
                     │
                    或者
                     │
                    */
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [self setStrokeColorAndWidth:context];
                    if (self.endGap.x < contentW) {
                        CGContextMoveToPoint(context, self.endGap.x, 0);
                        CGContextAddLineToPoint(context, contentW, 0);
                    } else {
                        CGContextMoveToPoint(context, contentW, 0);
                    }
                    CGContextAddLineToPoint(context, contentW, self.startGap.y);
                    CGContextStrokePath(context);
                }
            }
        }
    } else if (self.startGap.x == 0){
        /*
         ─────────┐
                  │
        └─────────┘
        或者
                  ┐
                  │
        └─────────┘
        */
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self setStrokeColorAndWidth:context];
        if (self.endGap.x < contentW) {
            CGContextMoveToPoint(context, self.endGap.x, 0);
            CGContextAddLineToPoint(context, contentW, 0);
        } else {
            CGContextMoveToPoint(context, contentW, 0);
        }
        CGContextAddLineToPoint(context, contentW, contentH);
        CGContextAddLineToPoint(context, 0, contentH);
        if (self.startGap.y > 0) {
            if (self.startGap.y < contentH) {
                CGContextAddLineToPoint(context, 0, self.startGap.y);
            }
        }
        CGContextStrokePath(context);
    }
}

- (void)drawLeftRect:(CGRect)rect {
    CGFloat contentW = CGRectGetWidth(rect);
    CGFloat contentH = CGRectGetHeight(rect);
    /*
     ───────
     ─────────┐
     ┌─────────
     ┌─────────┐
     ┌─────────┐
               │
             ──┘
     ┌─────────┐
               │
     └─────────┘
      ─────────┐
     │         │
     └─────────┘
     */
    if (self.endGap.y > 0) {
        if (self.startGap.y > 0) {
            if (self.startGap.y < contentH) {
                if (self.startGap.x > 0) {
                    if (self.startGap.x == contentW) {
                        CGContextRef context = UIGraphicsGetCurrentContext();
                        [self setStrokeColorAndWidth:context];
                        if (self.endGap.y < contentH) {
                            CGContextMoveToPoint(context, 0, self.endGap.y);
                        } else {
                            CGContextMoveToPoint(context, 0, contentH);
                        }
                        CGContextAddLineToPoint(context, 0, 0);
                        CGContextAddLineToPoint(context, contentW, 0);
                        CGContextAddLineToPoint(context, contentW, self.startGap.y);
                        CGContextStrokePath(context);
                    }
                } else if (self.startGap.x == 0) {
                    if (self.endGap.y >= contentH) {
                        CGContextRef context = UIGraphicsGetCurrentContext();
                        [self setStrokeColorAndWidth:context];
                        CGContextMoveToPoint(context, 0, 0);
                        CGContextAddLineToPoint(context, contentW, 0);
                        CGContextAddLineToPoint(context, contentW, contentH);
                        CGContextAddLineToPoint(context, 0, contentH);
                        CGContextClosePath(context);
                        CGContextStrokePath(context);
                    } else {
                        if (self.startGap.y > self.endGap.y) {
                            CGContextRef context = UIGraphicsGetCurrentContext();
                            [self setStrokeColorAndWidth:context];
                            CGContextMoveToPoint(context, 0, self.endGap.y);
                            CGContextAddLineToPoint(context, 0, 0);
                            CGContextAddLineToPoint(context, contentW, 0);
                            CGContextAddLineToPoint(context, contentW, contentH);
                            CGContextAddLineToPoint(context, 0, contentH);
                            if (self.startGap.y < contentH) {
                                CGContextAddLineToPoint(context, 0, self.startGap.y);
                            }
                            CGContextStrokePath(context);
                        } else if (self.startGap.y < self.endGap.y){
                            CGContextRef context = UIGraphicsGetCurrentContext();
                            [self setStrokeColorAndWidth:context];
                            CGContextMoveToPoint(context, 0, self.endGap.y);
                            CGContextAddLineToPoint(context, 0, self.startGap.y);
                            CGContextStrokePath(context);
                        }
                    }
                }
            } else {
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                if (self.endGap.y < contentH) {
                    CGContextMoveToPoint(context, 0, self.endGap.y);
                } else {
                    CGContextMoveToPoint(context, 0, contentH);
                }
                CGContextAddLineToPoint(context, 0, 0);
                CGContextAddLineToPoint(context, contentW, 0);
                CGContextAddLineToPoint(context, contentW, contentH);
                if (self.startGap.x > 0) {
                    if (self.startGap.x < contentW) {
                        CGContextAddLineToPoint(context, self.startGap.x, contentH);
                    }
                } else if (self.startGap.x == 0) {
                     CGContextAddLineToPoint(context, 0, contentH);
                }
                CGContextStrokePath(context);
            }
        } else if (self.startGap.y == 0) {
            CGContextRef context = UIGraphicsGetCurrentContext();
            [self setStrokeColorAndWidth:context];
            if (self.endGap.y < contentH) {
                CGContextMoveToPoint(context, 0, self.endGap.y);
            } else {
                CGContextMoveToPoint(context, 0, contentH);
            }
            CGContextAddLineToPoint(context, 0, 0);
            if (self.startGap.x > 0) {
                if (self.startGap.x < contentW) {
                    CGContextAddLineToPoint(context, self.startGap.x, 0);
                } else {
                    CGContextAddLineToPoint(context, contentW, 0);
                }
            }
            CGContextStrokePath(context);
        }
    } else if (self.endGap.y == 0){
        if (self.startGap.y >0) {
            if (self.startGap.y < contentH) {
                if (self.startGap.x == 0) {
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [self setStrokeColorAndWidth:context];
                    CGContextMoveToPoint(context, 0, 0);
                    CGContextAddLineToPoint(context, contentW, 0);
                    CGContextAddLineToPoint(context, contentW, contentH);
                    CGContextAddLineToPoint(context, 0, contentH);
                    CGContextAddLineToPoint(context, 0, self.startGap.y);
                    CGContextStrokePath(context);
                } else if (self.startGap.x == contentW) {
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [self setStrokeColorAndWidth:context];
                    CGContextMoveToPoint(context, 0, 0);
                    CGContextAddLineToPoint(context, contentW, 0);
                    CGContextAddLineToPoint(context, contentW, self.startGap.y);
                    CGContextStrokePath(context);
                }
            } else {
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                CGContextMoveToPoint(context, 0, 0);
                CGContextAddLineToPoint(context, contentW, 0);
                CGContextAddLineToPoint(context, contentW, contentH);
                if (self.startGap.x > 0) {
                    if (self.startGap.x < contentW) {
                        CGContextAddLineToPoint(context, self.startGap.x, contentH);
                    }
                } else if (self.startGap.x == 0) {
                    CGContextAddLineToPoint(context, 0, contentH);
                }
                CGContextStrokePath(context);
            }
        } else if (self.startGap.y == 0) {
            if (self.startGap.x > 0) {
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                CGContextMoveToPoint(context, 0, 0);
                if (self.startGap.x < contentW) {
                    CGContextAddLineToPoint(context, self.startGap.x, 0);
                } else {
                    CGContextAddLineToPoint(context, contentW, 0);
                }
                CGContextStrokePath(context);
            } else if (self.startGap.x == 0) {
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                CGContextMoveToPoint(context, 0, 0);
                CGContextAddLineToPoint(context, contentW, 0);
                CGContextAddLineToPoint(context, contentW, contentH);
                CGContextAddLineToPoint(context, 0, contentH);
                CGContextClosePath(context);
                CGContextStrokePath(context);
            }
        }
    }
}

- (void)drawRightRect:(CGRect)rect {
    CGFloat contentW = CGRectGetWidth(rect);
    CGFloat contentH = CGRectGetHeight(rect);
    if (self.startGap.x > 0) {
        if (self.startGap.y > 0) {
            if (self.startGap.y < contentH) {
                if (self.startGap.x == contentW) {
                    if (self.startGap.y < self.endGap.y) {
                        CGContextRef context = UIGraphicsGetCurrentContext();
                        [self setStrokeColorAndWidth:context];
                        if (self.endGap.y < contentH) {
                            CGContextMoveToPoint(context, contentW, self.endGap.y);
                            CGContextAddLineToPoint(context, contentW, contentH);
                        } else {
                            CGContextMoveToPoint(context, contentW, contentH);
                        }
                        CGContextAddLineToPoint(context, 0, contentH);
                        CGContextAddLineToPoint(context, 0, 0);
                        CGContextAddLineToPoint(context, contentW, 0);
                        CGContextAddLineToPoint(context, contentW, self.startGap.y);
                        CGContextStrokePath(context);
                    } else {
                        if (self.startGap.y == self.startGap.y) {
                            CGContextRef context = UIGraphicsGetCurrentContext();
                            [self setStrokeColorAndWidth:context];
                            CGContextMoveToPoint(context, 0, 0);
                            CGContextAddLineToPoint(context, contentW, 0);
                            CGContextAddLineToPoint(context, contentW, contentH);
                            CGContextAddLineToPoint(context, 0, contentH);
                            CGContextClosePath(context);
                            CGContextStrokePath(context);
                        } else {
                            if (self.endGap.y < contentH) {
                                CGContextRef context = UIGraphicsGetCurrentContext();
                                [self setStrokeColorAndWidth:context];
                                CGContextMoveToPoint(context, contentW, self.endGap.y);
                                if (self.startGap.y >= contentH) {
                                    CGContextAddLineToPoint(context, contentW, contentH);
                                } else {
                                    CGContextAddLineToPoint(context, contentW, self.startGap.y);
                                }
                                CGContextStrokePath(context);
                            }
                        }
                    }
                }
            } else {
                if (self.startGap.x < contentW) {
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [self setStrokeColorAndWidth:context];
                    if (self.endGap.y < contentH) {
                        CGContextMoveToPoint(context, contentW, self.endGap.y);
                        CGContextAddLineToPoint(context, contentW, contentH);
                    } else {
                        CGContextMoveToPoint(context, contentW, contentH);
                    }
                    CGContextAddLineToPoint(context, self.startGap.x, contentH);
                    CGContextStrokePath(context);
                } else {
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [self setStrokeColorAndWidth:context];
                    CGContextMoveToPoint(context, 0, 0);
                    CGContextAddLineToPoint(context, contentW, 0);
                    CGContextAddLineToPoint(context, contentW, contentH);
                    CGContextAddLineToPoint(context, 0, contentH);
                    CGContextClosePath(context);
                    CGContextStrokePath(context);
                }
            }
        } else if (self.startGap.y == 0) {
            if (self.startGap.x < contentW) {
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                if (self.endGap.y < contentH) {
                    CGContextMoveToPoint(context, contentW, self.endGap.y);
                    CGContextAddLineToPoint(context, contentW, contentH);
                } else {
                    CGContextMoveToPoint(context, contentW, contentH);
                }
                CGContextAddLineToPoint(context, 0, contentH);
                CGContextAddLineToPoint(context, 0, 0);
                CGContextAddLineToPoint(context, self.startGap.x, 0);
                CGContextStrokePath(context);
            } else {
                if (self.endGap.y > 0) {
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [self setStrokeColorAndWidth:context];
                    if (self.endGap.y < contentH) {
                        CGContextMoveToPoint(context, contentW, self.endGap.y);
                    } else {
                        CGContextMoveToPoint(context, contentW, contentH);
                    }
                    CGContextAddLineToPoint(context, contentW, self.startGap.y);
                    CGContextStrokePath(context);
                } else if (self.endGap.y == 0) {
                    CGContextRef context = UIGraphicsGetCurrentContext();
                    [self setStrokeColorAndWidth:context];
                    CGContextMoveToPoint(context, 0, 0);
                    CGContextAddLineToPoint(context, contentW, 0);
                    CGContextAddLineToPoint(context, contentW, contentH);
                    CGContextAddLineToPoint(context, 0, contentH);
                    CGContextClosePath(context);
                    CGContextStrokePath(context);
                }
            }
        }
    } else if (self.startGap.x == 0){
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self setStrokeColorAndWidth:context];
        if (self.endGap.y < contentH) {
            CGContextMoveToPoint(context, contentW, self.endGap.y);
            CGContextAddLineToPoint(context, contentW, contentH);
        } else {
            CGContextMoveToPoint(context, contentW, contentH);
        }
        CGContextAddLineToPoint(context, 0, contentH);
        if (self.startGap.y < contentH) {
            CGContextAddLineToPoint(context, 0, self.startGap.y);
        }
        CGContextStrokePath(context);
    }
}

- (void)drawBottomRect:(CGRect)rect {
    CGFloat contentW = CGRectGetWidth(rect);
    CGFloat contentH = CGRectGetHeight(rect);
    if (self.startGap.y > 0) {
        if (self.startGap.y < contentH) {
            if (self.startGap.x == 0) {
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                CGContextMoveToPoint(context, self.endGap.x, contentH);
                CGContextAddLineToPoint(context, 0, contentH);
                CGContextAddLineToPoint(context, 0, self.startGap.y);
                CGContextStrokePath(context);
            }
        } else {
            if (self.startGap.x > 0) {
                if (self.startGap.x < contentH) {
                    if (self.startGap.x > self.endGap.x) {
                        CGContextRef context = UIGraphicsGetCurrentContext();
                        [self setStrokeColorAndWidth:context];
                        CGContextMoveToPoint(context, self.endGap.x, contentH);
                        CGContextAddLineToPoint(context, 0, contentH);
                        CGContextAddLineToPoint(context, 0, 0);
                        CGContextAddLineToPoint(context, contentW, 0);
                        CGContextAddLineToPoint(context, contentW, contentH);
                        CGContextAddLineToPoint(context, self.startGap.x, contentH);
                        CGContextStrokePath(context);
                    } else {
                        if (self.startGap.x == self.endGap.x) {
                            CGContextRef context = UIGraphicsGetCurrentContext();
                            [self setStrokeColorAndWidth:context];
                            CGContextMoveToPoint(context, 0, 0);
                            CGContextAddLineToPoint(context, contentW, 0);
                            CGContextAddLineToPoint(context, contentW, contentH);
                            CGContextAddLineToPoint(context, 0, contentH);
                            CGContextClosePath(context);
                            CGContextStrokePath(context);
                        } else {
                            CGContextRef context = UIGraphicsGetCurrentContext();
                            [self setStrokeColorAndWidth:context];
                            CGContextMoveToPoint(context, self.endGap.x, contentH);
                            CGContextAddLineToPoint(context, self.startGap.x, contentH);
                            CGContextStrokePath(context);
                        }
                    }
                } else {
                    if (self.endGap.x < contentH) {
                        CGContextRef context = UIGraphicsGetCurrentContext();
                        [self setStrokeColorAndWidth:context];
                        CGContextMoveToPoint(context, self.endGap.x, contentH);
                        CGContextAddLineToPoint(context, 0, contentH);
                        CGContextAddLineToPoint(context, 0, 0);
                        CGContextAddLineToPoint(context, contentW, 0);
                        CGContextAddLineToPoint(context, contentW, contentH);
                        CGContextStrokePath(context);
                    } else {
                        CGContextRef context = UIGraphicsGetCurrentContext();
                        [self setStrokeColorAndWidth:context];
                        CGContextMoveToPoint(context, 0, 0);
                        CGContextAddLineToPoint(context, contentW, 0);
                        CGContextAddLineToPoint(context, contentW, contentH);
                        CGContextAddLineToPoint(context, 0, contentH);
                        CGContextClosePath(context);
                        CGContextStrokePath(context);
                    }
                }
            } else if (self.startGap.x == 0){
                CGContextRef context = UIGraphicsGetCurrentContext();
                [self setStrokeColorAndWidth:context];
                if (self.endGap.x < contentW) {
                    CGContextMoveToPoint(context, self.endGap.x, contentH);
                } else {
                    CGContextMoveToPoint(context, contentW, contentH);
                }
                CGContextAddLineToPoint(context, 0, contentH);
                CGContextStrokePath(context);
            }
        }
    } else if (self.startGap.y == 0) {
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self setStrokeColorAndWidth:context];
        if (self.endGap.x < contentW) {
            CGContextMoveToPoint(context, self.endGap.x, contentH);
        } else {
            CGContextMoveToPoint(context, contentW, contentH);
        }
        CGContextAddLineToPoint(context, 0, contentH);
        CGContextAddLineToPoint(context, 0, 0);
        if (self.startGap.x > 0) {
            if (self.startGap.x < contentW) {
                CGContextAddLineToPoint(context, self.startGap.x, 0);
            } else {
                CGContextAddLineToPoint(context, contentW, 0);
            }
        }
        CGContextStrokePath(context);
    }
}

- (void)drawRect:(CGRect)rect {
    if (self.startGap.x >= 0 && self.startGap.y >= 0 && self.endGap.x >= 0 && self.endGap.y >= 0 && !CGPointEqualToPoint(self.startGap, self.endGap)){
        CGFloat contentW = CGRectGetWidth(rect);
        CGFloat contentH = CGRectGetHeight(rect);
        if (self.endGap.x > 0) {
            if (self.endGap.y > 0) {
                if (self.endGap.y == contentH) {
                    // 底边
                    [self drawBottomRect:rect];
                } else if (self.endGap.x == contentW){
                    // 右边
                    [self drawRightRect:rect];
                }
            } else if (self.endGap.y == 0){
                //上边
                [self drawTopRect:rect];
            }
        } else if (self.endGap.x == 0){
            //左边
            [self drawLeftRect:rect];
        }
    }
}

@end
